using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.IService.Sys;

namespace net_work.Controllers.Sys;

[Route("api/[controller]")]
[ApiController]
public class StudentController(ILogger<StudentController> logger, IStudentService studentService) : ControllerBase
{
    /// <summary>
    /// 获取学生列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListStudents([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取学生列表");
        var result = await studentService.GetStudentsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取学生信息
    /// </summary>
    /// <param name="studentId">学生ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetStudentById([FromQuery] long studentId)
    {
        logger.LogInformation("获取学生信息");
        return await studentService.SelectByIdAsync(studentId) is { } student
            ? AjaxResult.Success(student)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增学生
    /// </summary>
    /// <param name="studentAddRequest">新增学生实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddStudent([FromBody] StudentAddRequest studentAddRequest)
    {
        logger.LogInformation("新增学生");
        var studentId = await studentService.InsertStudent(studentAddRequest);
        return studentId > 0
            ? AjaxResult.Success(studentId)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改学生
    /// </summary>
    /// <param name="studentUpdateRequest">修改学生实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> UpdateStudent([FromBody] StudentUpdateRequest studentUpdateRequest)
    {
        logger.LogInformation("修改学生");
        return await studentService.UpdateStudent(studentUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除学生
    /// </summary>
    /// <param name="ids">学生ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeleteStudent([FromBody] List<long> ids)
    {
        logger.LogInformation("删除学生");
        return await studentService.DeleteStudentByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}